export default {
  namespaced: true,
  state: {
    isLogin: JSON.parse(sessionStorage.getItem("isLogin")) || false,
    username: sessionStorage.getItem("username") || "",
    user: JSON.parse(sessionStorage.getItem("user")) || {},
  },
  mutations: {
    login(state) {
      state.isLogin = true;
      sessionStorage.setItem("isLogin", true);
    },

    logout(state) {
      state.isLogin = false;
      sessionStorage.setItem("isLogin", false);
    },

    setUserName(state, username) {
      state.username = username;
      sessionStorage.setItem("username", username);
    },
    setUser(state, user) {
      state.user = user;
      sessionStorage.setItem("user", user);
    },
  },
  actions: {
    login({ commit }, username) {
      setTimeout(() => {
        commit("login");
        commit("setUserName", username);
        commit("setUser", user);
      }, 1000);
    },
  },
  getters: {
    welcome: (state) => state.username + "欢迎你",
  },
};
